package com.ghostwan.androears.adapter;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ghostwan.androears.AndroEars;
import com.ghostwan.androears.R;
import com.ghostwan.androears.customObject.Song;

public class PlayListAdapter extends BaseAdapter{
		private final LayoutInflater mInflater;
		private final int mLayoutRes;
		private ArrayList<Song> mSongList;
		public AndroEars mAndroEars;
		protected String TAG = "PlayListAdapter";
		
		public PlayListAdapter(AndroEars androEars, int textViewResourceId) {
			super();
			mAndroEars = androEars;
			mInflater = (LayoutInflater)mAndroEars.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mLayoutRes = textViewResourceId;
			mSongList = new ArrayList<Song>();
		}
		
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view;
			if( convertView == null)
			{
				view = mInflater.inflate(mLayoutRes, parent, false);
			}
			else
			{
				view = convertView;
			}
			Song song = this.getItem(position);
			LinearLayout removeLayout = (LinearLayout) view.findViewById(R.id.removeLayout);
			removeLayout.setClickable(true);
			removeLayout.setTag(new Integer(position));
			// addLayout.setFocusable(true);
			removeLayout.setBackgroundResource(android.R.drawable.menuitem_background);
			removeLayout.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					try
					{
						Song song = mSongList.get((Integer) v.getTag());
						mAndroEars.mPlayerService.removeSongPlayList((Integer) v.getTag());
						if (mAndroEars.mPlayerService.getSongList().size() == 0)
						{
							mAndroEars.mMusicPlayerButton.setVisibility(View.INVISIBLE);
							mAndroEars.displayAlbumList();
						}
						else
						{
							mAndroEars.displayPlayList();
						}
						String s = new String();
						s =  mAndroEars.getString(R.string.remouving)+" "
						+song.mSongName;
						Toast.makeText(mAndroEars,s,Toast.LENGTH_LONG).show(); 
					}
					catch (RemoteException e)
					{
						Log.e(TAG, e.getMessage());
					}
					
				}
			});
			TextView songDuration = (TextView)view.findViewById(R.id.songDuration);
			((TextView)view.findViewById(R.id.songName)).setText(song.mSongName);
			((TextView)view.findViewById(R.id.artistName)).setText(song.mArtistName);
			if(song.mIsPlayed == 1) ((ImageView)view.findViewById(R.id.listening)).setVisibility(ImageView.VISIBLE);
			else ((ImageView)view.findViewById(R.id.listening)).setVisibility(ImageView.INVISIBLE);
				
			long z = song.mDuration;
			long s = (z/1000)%60;
			long m = (z/60000);
			NumberFormat formatter = new DecimalFormat("00");
			songDuration.setText(formatter.format(m)+":"+formatter.format(s));
			ImageView songCover = (ImageView)view.findViewById(R.id.songCover);
			
			if(song.mSongCover != null)
			{
		        songCover.setImageURI(Uri.parse(song.mSongCover));		
			}
			else
			{
				songCover.setImageResource(R.drawable.song_default_black);
			}
				
			return view;
		}


		public void setSongList(ArrayList<Song> list)
		{
			mSongList = list;
			this.notifyDataSetChanged();
		}
		@Override
		public int getCount()
		{
			return mSongList.size();
		}


		@Override
		public Song getItem(int position)
		{
			return mSongList.get(position);
		}


		@Override
		public long getItemId(int position)
		{
			return position;
		}
		
	}

